package Conexion;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;



public class Conexion
{
    public static Connection con;
    public static boolean loggedIn = false;
    private static Conexion ConexionConectada = null;
    protected ResultSet rs;
    private String usr,pass;

    public Conexion(String usr, String pass)
    {
        this.usr = usr;
        this.pass = pass;
        ifConnected();
    }

    private void ifConnected()
    {
        try
        {
            if (Conexion.loggedIn == true)
            {
                con = Conexion.getDatabaseConnection().getConnection();
            }
            else
            {
                Conectar();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static Conexion getDatabaseConnection()
    {
        return Conexion.ConexionConectada;
    }

    private static void setDatabaseConnection(Conexion conexion)
    {
        Conexion.ConexionConectada = conexion;
    }

        
    public Connection getConnection()
    {
        return con;
    }

    public void EjecutarSentencia(String sentencia) throws SQLException
    {
        ifConnected();
        PreparedStatement ps;
        ps = con.prepareStatement(sentencia);
        ps.execute();
    }
    
    public void EjecutarUpdate(String sentencia)
    {
        ifConnected();
        PreparedStatement ps;
        try
        {
            ps = con.prepareStatement(sentencia);
            ps.executeUpdate();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
    
    public void Close()
    {
        try
        {
            loggedIn=false;
            con.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public ResultSet EjecutarConsulta(String consulta)
    {
        ifConnected();
        PreparedStatement ps;
        try
        {
            ps = con.prepareStatement(consulta);
            rs = ps.executeQuery();
            return rs;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public void Conectar()
    {
        try
        {
            //SQL SERVER Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); //Oracle
            // MySQL Class.forName("com.mysql.jdbc.Driver").newInstance();
            Login();

        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
        }
    }

    private void Login()
    {        
        try
        {
            //String url = "jdbc:mysql://s156.eatj.com:3306/ruddypazde";
            //con = DriverManager.getConnection(url, "ruddypazde", "RRPdrjhsad");            
            //con = DriverManager.getConnection("jdbc:sqlserver://192.168.1.102:1433;databaseName=apuestasMundial", "sa", "RRPdrjhsad2014*");
            con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.102:1521:broker", usr, pass);
            
            Conexion.loggedIn = true;
            Conexion.setDatabaseConnection(this);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }        

    public static boolean isLoggedIn() {
        return loggedIn;
    }

    public static void setLoggedIn(boolean loggedIn) {
        Conexion.loggedIn = loggedIn;
    }
    
    
}
